const { Op } = require('sequelize');
const Category = require('../../models/product/category.model');
const sendResponse = require('../../utils/response');

// 获取一级分类
exports.getCategory1 = async (req, res, next) => {
  try{
    const category1 = await Category.findAll({
      where: {
        level: 1
      },
      attributes: ['id', 'name', 'praent_id', 'level']
    });
    console.log('--------获取一级分类列表-------');
    return sendResponse(res, { data: category1 });
  } catch (error) {
    next(error)
  }
}
// 获取二级分类
exports.getCategory2 = async (req, res, next) => {
  try{
    // 获取一级分类id
    const { category1Id } = req.params;
    // 获取二级分类列表
    const category2 = await Category.findAll({
      where: {
        praent_id: category1Id
      },
      attributes: ['id', 'name', 'praent_id', 'level']
    });
    console.log('--------获取二级分类列表-------');
    return sendResponse(res, { data: category2 });
  } catch (error) {
    next(error)
  }
}
// 获取三级分类
exports.getCategory3 = async (req, res, next) => {
  try{
    const { category2Id } = req.params;
    // 获取三级分类列表
    const category3 = await Category.findAll({
      where: {
        praent_id: category2Id
      },
      attributes: ['id', 'name', 'praent_id', 'level']
    });
    console.log('--------获取三级分类列表-------');
    return sendResponse(res, { data: category3 })
  } catch (error) {
    next(error)
  }
}
// 获取全部分类
exports.getCategoryAll = async (req, res, next) => {
  try{
    const categoryAll = await Category.findAll({
      where: {
        praent_id: { 
          [Op.or]: [0, null],
        } // 顶级分类条件
      },
      order: [
        ['id', 'ASC']
      ],
      attributes: ['id', 'name', 'praent_id', 'level'],
      include: [{
        model: Category,
        as: 'children',
        attributes: ['id', 'name', 'praent_id', 'level'],
        include: [{
          model: Category,
          as: 'children',
          attributes: ['id', 'name', 'praent_id', 'level']
        }]
      }]
    });
    console.log('--------获取全部分类列表-------');
    return sendResponse(res, { data: categoryAll })
  } catch (error) {
    next(error)
  }
}
// 新增分类
exports.saveCategory = async (req, res, next) => {
  try{
    const { praent_id, name, level } = req.body
    // console.log(praent_id);
    
    if (!name){
      return sendResponse(res, { code: 400, message: '参数错误' })
    }

    // 新增分类
    Category.create({
      praent_id,
      name,
      level
    })
    
    console.log("------新增分类成功-------");
    return sendResponse(res, { message: '新增成功' })
  } catch (error) {
    next(error)
  }
}
// 删除分类
exports.removeCategory = async (req, res, next) => {
  try{
    const { id } = req.params;
    // 先查询分类下是否有子分类
    const category = await Category.findOne({
      where: {
        praent_id: id
      }
    })
    if(category) {
      // TODO 如果要删除所有子分类，可在这里写删除逻辑，通过递归删除所有子分类
      return sendResponse(res, { code: 400, message: '该分类下有子分类，无法删除' })
    }
    // 删除分类
    const result = await Category.destroy({
      where: {
        id
      }
    })
    
    if (result === 0) {
      return sendResponse(res, { code: 400, message: '删除失败' })
    }
    console.log("------删除分类成功-------");
    return sendResponse(res, { message: '删除成功' })
  } catch (error) {
    next(error)
  }
}
// 更新分类
exports.updateCategory = async (req, res, next) => {
  try{
    const { id, name, praent_id, level } = req.body
    // 更新分类
    if(!id || !name){
      return sendResponse(res, { code: 400, message: '参数错误' })
    }
    Category.update({
      name,
      praent_id,
      level
    }, {
      where: {
        id
      }
    })
    console.log("------更新分类成功-------");
    return sendResponse(res, { message: '更新成功' })
  } catch (error) {
    next(error)
  }
}
